package com.duoqin.chat.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;

import com.duoqin.chat.Chat;
import com.duoqin.common.ui.AbsActivity;
import com.duoqin.emoji.EmojiManager;
import com.duoqin.syncassistant.R;

public class ReadMessageActivity extends AbsActivity {
    public final static String INTENT_EXTRA_CHAT = "chat";
    public final static String INTENT_EXTRA_FAVORITE = "favorite";

    private Chat mChat;
    private TextView mMessageTV;
    private AlertDialog mAlertDialog;
    private boolean mIsFavorite;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        setContentView(R.layout.activity_chat_read_message);
        setupTitle(getString(R.string.message_detail));

        mMessageTV = findViewById(R.id.message);

        mChat = getIntent().getParcelableExtra(INTENT_EXTRA_CHAT);
        if (mChat != null) {
            mMessageTV.setText(EmojiManager.getInstance().getExpressionString(
                    getApplicationContext(), mChat.message_text,
                    getResources().getDimensionPixelSize(R.dimen.duoqin_text_size_medium)));
        }
        mIsFavorite = getIntent().getBooleanExtra(INTENT_EXTRA_FAVORITE, false);
    }

    protected void setupTitle(String title) {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(title);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_MENU: {
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setMessage(R.string.delete_confirm_notice)
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (mIsFavorite) {
                                    Chat.delete(getContentResolver(),
                                            Chat.Columns.FAVORITE_CONTENT_URI, mChat);
                                } else {
                                    Chat.delete(getContentResolver(),
                                            Chat.Columns.CONTENT_URI, mChat);
                                }
                                finish();
                            }
                        })
                        .setNegativeButton(android.R.string.cancel, null);
                mAlertDialog = builder.create();
                mAlertDialog.show();
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAlertDialog != null) {
            mAlertDialog.dismiss();
        }
    }
}
